import org.apache.commons.lang3.StringUtils;
import org.jasypt.util.text.AES256TextEncryptor;

/**
 * 文本加密解密demo
 * 示例：第三方工具jasypt的使用
 *
 * @author Zhou
 */
public class JasyptDemo {
    private static final String passwd = "xxxx";

    public static void main(String[] args) {
        encrypt("");
    }

    public static void decrypt(String str) {
        if (StringUtils.isEmpty(str)) return;

        AES256TextEncryptor textEncryptor = new AES256TextEncryptor();
        //加密所需的salt
        textEncryptor.setPassword(passwd);
        var decStr = textEncryptor.decrypt(str);
        System.out.println("decStr: " + decStr);
    }

    /**
     * 账号密码加密
     */
    public static void encrypt(String str) {
        if (StringUtils.isEmpty(str)) return;

        AES256TextEncryptor textEncryptor = new AES256TextEncryptor();
        //加密所需的salt
        textEncryptor.setPassword(passwd);

        //要加密的数据（数据库的用户名或密码）
        var encStr = textEncryptor.encrypt(str);

        System.out.println("encStr: " + encStr);
    }
}
